package com.heima.wemedia.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.heima.wemedia.config.RabbitMQConfig.*;

@Component
@RequiredArgsConstructor
public class WmNewsListener {
private final WmNewsService wmNewsService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = DELAYED_QUEUE),
            exchange = @Exchange(name = DELAYED_EXCHANGE, delayed = "true"),
            key = DELAYED_ROUTING_KEY
    ))
    public void publishWmNews(String id){

        Long l = Long.valueOf(id);
        WmNews byId = wmNewsService.getById(l);
        if(byId.getStatus()==9)return;
        LambdaUpdateWrapper<WmNews> lam=new LambdaUpdateWrapper<>();
        lam.eq(WmNews::getId,l).set(WmNews::getStatus,9).set(WmNews::getPublishTime,new Date());
        wmNewsService.update(lam);
    }
}
